

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_my_app/provider/todo_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'models/todo.dart';

//现在我们已经定义了一个ChangeNotifierProvider，我们可以使用它来与 UI 中的待办事项列表进行交互：
class TodoListView extends ConsumerWidget {
  const TodoListView({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // TODO: implement build
    List<Todo> todos = ref.watch(todosProvider).todos;
    // Let's render the todos in a scrollable list view
    return Column(
        children: [
          GestureDetector(
              onTap: (){
                  ref.read(todosProvider).todos.add(Todo(id: "id1", description: "todosProvider", completed: true));
              },
              child: Text("点击")),
          ListView(
            children: [
              for (final todo in todos)
                CheckboxListTile(
                  value: todo.completed,
                  // When tapping on the todo, change its completed status
                  onChanged: (value) =>
                      ref.read(todosProvider.notifier).toggle(todo.id),
                  title: Text("文本描述 ${todo.description}"),
                ),
            ],
          )
        ],
    );
  }
}